package t327.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import t327.pojo.*;
import t327.pojo.font.LuiTableEntity;
import t327.pojo.views.PlanOrderProductionStyleView;
import t327.pojo.views.SubmissionOrderProductStyleDepartmentSizeView;
import t327.service.iter.ISewingManagerService;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("sewingManager")
public class SewingManageController {
    @Autowired
    private ISewingManagerService sewingManagerService;

    @RequestMapping("getAllPlanDepartmentSize")
    public LuiTableEntity getAllPlanDepartmentSize(Integer page, Integer limit){
        PageHelper.startPage(page,limit);
        LuiTableEntity luiTableEntity = new LuiTableEntity<Expend>();
        PageInfo<PlanOrderProductionStyleView> planDepartmentSizeView = new PageInfo<PlanOrderProductionStyleView>(sewingManagerService.getAllPlanOrderSizeView());
        List<PlanOrderProductionStyleView> planOrderSizeViewList = planDepartmentSizeView.getList();
        long total = planDepartmentSizeView.getTotal();
        if(planOrderSizeViewList !=null&& planOrderSizeViewList.size()>0){
            luiTableEntity.setCode("0");
            luiTableEntity.setMsg("查询成功");
            luiTableEntity.setCount(total);
            luiTableEntity.setData(planOrderSizeViewList);
        }else{
            luiTableEntity.setCode("203");
            luiTableEntity.setMsg("暂无数据");
        }
        return   luiTableEntity;
    }
    @RequestMapping("queryAllSubmission")
    public LuiTableEntity queryAllSubmission(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        LuiTableEntity luiTableEntity = new LuiTableEntity<Expend>();
        PageInfo<SubmissionOrderProductStyleDepartmentSizeView> submissionOcsrViewPageInfo = new PageInfo<>(sewingManagerService.queryAllSubmission());
        List<SubmissionOrderProductStyleDepartmentSizeView> SubmissionOcsrViewList = submissionOcsrViewPageInfo.getList();
        long total = submissionOcsrViewPageInfo.getTotal();
        if(SubmissionOcsrViewList !=null&& SubmissionOcsrViewList.size()>0){
            luiTableEntity.setCode("0");
            luiTableEntity.setMsg("查询成功");
            luiTableEntity.setCount(total);
            luiTableEntity.setData(SubmissionOcsrViewList);
        }else{
            luiTableEntity.setCode("203");
            luiTableEntity.setMsg("暂无数据");
        }
        return   luiTableEntity;
    }
    @RequestMapping("addSubmission")
    public HashMap<String,String> addSubmission(@RequestBody Submission submission){
        HashMap<String,String> map = new HashMap<>();
        Long producedCount = submission.getProducedCount();
        if (producedCount == null || producedCount.equals("")){
            map.put("code","201");
            map.put("msg","一天没生产吗?????");
            return map;
        }
        if (producedCount <0){
            map.put("code","202");
            map.put("msg","不能为负数");
            return map;
        }
        if (!isNumeric(producedCount.toString())){
            map.put("code","203");
            map.put("msg","数量只能为数字");
            return map;
        }
        return sewingManagerService.addSubmission(submission);
    }
    //验证是单价和总价是否是数字
    public boolean isNumeric(String input) {
        try {
            Long.parseLong(input);  // 尝试将输入解析为整数
            // 或
            // Double.parseDouble(input);  // 尝试将输入解析为浮点数
            return true;  // 解析成功，输入为数字
        } catch (NumberFormatException e) {
            return false;  // 解析失败，输入不是数字
        }
    }

    //渲染页面的缝纫实际生产计划
    @RequestMapping("renderTheActualProductionPlanOfTheSewingPage")
    public List<SubmissionOrderProductStyleDepartmentSizeView> renderTheActualProductionPlanOfTheSewingPage(){
      return   sewingManagerService.queryAllSubmission();
    }

    //渲染页面的缝纫实际生产计划
    @RequestMapping("queryTheNumberOfCropProduction")
    public List<Submission> queryTheNumberOfCropProduction(){
        return   sewingManagerService.QueryTheNumberOfCropProduction();
    }
}
